//路由鉴权：就是路由能不能被访问到的权限设置-->全局守卫
//引入路由器文件
import router from './router'
//引入进度条
//@ts-ignore
import Nprogress from 'nprogress'
//引入进度条的样式
import 'nprogress/nprogress.css'
//引入用户仓库
import useUserStore from './store/modules/user'

//不需要进度条加载的小圆球
Nprogress.configure({ showSpinner: false })
//用户仓库
let userStore = useUserStore()
//未登录可以访问的路由
let whiteList = ['/home', '/hospital/register', '/hospital/detail', '/hospital/notice', '/hospital/close', '/hospital/search']

//前置路由守卫
router.beforeEach((to, from, next) => {
  //进度条开始
  Nprogress.start()
  //动态设置网站左上角的标题
  document.title = `尚医通-${to.meta.title}`
  //判断用户是否登录
  let token = userStore.userInfo.token
  if (token) {
    next()
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      //登陆组件显示
      userStore.visible = true
      //跳转到原本要去的页面
      next({
        path: '/home',
        query: {
          redirect: to.fullPath
        }
      })
    }
  }
})

//后置路由守卫
router.afterEach((to, from) => {
  //进度条结束
  Nprogress.done()

})